#include "QtExcelEditor.h"

QtExcelEditor::QtExcelEditor(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
}

QtExcelEditor::~QtExcelEditor()
{}

bool QtExcelEditor::ExcelApplication(const std::string& _excelPath)
{
    // 加载Excel驱动
    excel_ = new QAxObject("Excel.Application");
    if (!excel_)
        return false;

    // 默认隐藏
    excel_->setProperty("Visible", false);

    // 获取工作表集合
    workBooks_ = excel_->querySubObject("Workbooks");
    if (!workBooks_)
        return false;

    workBooks_->dynamicCall("Open(const QString&)", StdString2QString(_excelPath));

    workBook_ = excel_->querySubObject("ActiveWorkBook");
    if (!workBook_)
        return false;

	workSheet_ = workBook_->querySubObject("Sheets(int)", 1);
	if (!workSheet_)
		return false;

    return true;
}

bool QtExcelEditor::SetReadWorkSheet(const int& _num)
{
    if (!workBook_)
        return false;

    workSheet_ = workBook_->querySubObject("Sheets(int)", _num);
    if (!workSheet_)
        return false;

    return true;
}

bool QtExcelEditor::GetSheetCount(int& _sheetNum)
{
    if (!workBook_)
        return false;

	QAxObject* workSheets = workBook_->querySubObject("Sheets");
    if (!workSheets)
        return false;

    _sheetNum = workSheets->property("Count").toInt();

    delete workSheets;
    workSheets = nullptr;

    return true;
}

bool QtExcelEditor::GetRowColCount(int& _rows, int& _cols)
{
    if (!workSheet_)
        return false;

    // sheet表范围
    QAxObject* usedrRange = workSheet_->querySubObject("UsedRange");
    if (!usedrRange)
        return false;

	QAxObject* rows = usedrRange->querySubObject("Rows");
    if (!rows)
        return false;

    // 行数
    _rows = rows->property("Count").toInt();

	QAxObject* column = usedrRange->querySubObject("Columns");
    if (!column)
        return false;

    // 列数
    _cols = column->property("Count").toInt();

	/*delete usedrRange;
	usedrRange = nullptr;*/

	/*delete rows;
	rows = nullptr;

	delete column;
	column = nullptr;*/

    return true;
}

QString QtExcelEditor::QueryDataAt(const int& _row, const int& _col)
{
    if (!workSheet_)
        return "";

	QAxObject* cell = workSheet_->querySubObject("Cells(int,int)", _row, _col);
    if (!cell)
        return "";

	QVariant value = cell->property("Value");

	delete cell;
	cell = nullptr;

    //return QString2StdString(value.toString());
    return value.toString();
}

std::vector<std::vector<QString>> QtExcelEditor::QueryAllData()
{
    // 获取行数和列数
	int row = 0, col = 0;
	if (!GetRowColCount(row, col))
		return {};

	std::vector<std::vector<QString>> data(row, std::vector<QString>(col));

	for (int i = 1; i <= row; i++)
		for (int j = 1; j <= col; j++)
			data[i - 1][j - 1] = QueryDataAt(i, j);

	return data;
}

bool QtExcelEditor::WriteDataAt(const int& _row, const int& _col, const std::string& _value)
{
	if (!workSheet_)
		return false;

	QAxObject* Range = workSheet_->querySubObject("Cells(int,int)", _row, _col);
	Range->setProperty("ColumnWidth", 15);//设置列宽，避免出现有些列对应的距离太短，导致无法看清
    Range->dynamicCall("SetValue(const QString &)", StdString2QString(_value));

    return true;
}

bool QtExcelEditor::WriteDataRange(const std::string& _start, const std::string& _end, const RangeDate& _value)
{
	QVariantList vars;//行数据
	QString str = QString("%1:%2").arg(StdString2QString(_start)).arg(StdString2QString(_end));//范围
	const int m_rows = _value.size();//行数
	
    for (int i = 0; i < m_rows; ++i)
	{
		QList<QVariant> m_column_num;//每列数据

        for (int j = 0; j < _value[i].size(); j++)
			m_column_num.append(StdString2QString(_value[i][j], 'g', 7));

		vars.append(QVariant(m_column_num));
	}
	
    QAxObject* rangeAx = workSheet_->querySubObject("Range(const QString&)", str);
	rangeAx->dynamicCall("Value", QVariant(vars));

    return true;
}

bool QtExcelEditor::CloseExcel()
{
    workBook_->dynamicCall("Save()");
	workBook_->dynamicCall("Close(Boolean)", true);  // 关闭文件
	excel_->dynamicCall("Quit(void)");  // 退出

    return true;
}
